﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GRWL.BZSC.UI.BaseDataSvc;
using System.Collections;
using GRWL.BZSC.UI.CustomCtrl;

namespace GRWL.BZSC.UI
{
    public partial class UserFrm : BaseFrm
    {
        //BaseFrm中已有声明
        //public CodeRuleFrm.CodeRule codeRule { get; set; }

        public SelectItem SelectorItem { get; set; }

        public GRWL.BZSC.Common.EnumCommon.OptionType CurrentType { get; set; }

        public UserFrm()
        {
            InitializeComponent();
            //this.BaseDataProxy = new BaseDataClient();
            //this.BaseDataProxy.Open();
            //this.codeRule = new CodeRuleFrm.CodeRule();

            this.BaseDataProxy = new GRWL.BZSC.UI.BaseDataSvc.BaseDataClient();
            this.BaseDataProxy.Open();
            //this.CurrentType = GRWL.BZSC.Common.EnumCommon.OptionType.物料分类;
            this.codeRule = new CodeRuleFrm.CodeRule();
            //GRWL.BZSC.Common.EventCommon.OptionUdTVEvnt += new GRWL.BZSC.Common.EventCommon.UpdateTreeViewDlgt(updateTreeView);
        }

        private void UserFrm_Load(object sender, EventArgs e)
        {
            GRWL.BZSC.Common.EventCommon.UserUdDGVEvnt += new GRWL.BZSC.Common.EventCommon.UpdateDataGridViewDlgt(updateDgv);
            //GRWL.BZSC.Common.EventCommon.DeptUdTVEvnt += new GRWL.BZSC.Common.EventCommon.UpdateTreeViewDlgt(updateTreeView);
            BindTreeView();
            bindGV(null);
            this.codeRule.autoCode = false;
        }

        private void updateTreeView(object o, EventArgs arg)
        {
           TreeNode node = new TreeNode();
           GRWL.BZSC.UI.Common.OperateType opType = Common.OperateType.Add;
           CommonEventArg<TB_Department> argObj= (CommonEventArg<TB_Department>)arg;
           node.Name = argObj.t.id.ToString();
           node.Text = argObj.t.name;

           if (opType == Common.OperateType.Add)
               this.uTreeView1.SelectedNode.Nodes.Add(node);
           else if (opType == Common.OperateType.Update)
               this.uTreeView1.SelectedNode.Text = node.Text;
        }

        private void BindTreeView()//绑定部门列表
        {
            TB_Department[] tb_Department = this.BaseDataProxy.FindDepartments(null,null,null);
            Common.BindDeptTree(uTreeView1, tb_Department, "部门列表");
        }

        private void bindGV(int? depot)//绑定用户信息列表e.Node.Name
        {
            this.dgvData.Rows.Clear();
            XUser[] objAry = this.BaseDataProxy.FindUsers(depot, this.textBox1.Text, this.textBox2.Text);
            if (objAry != null)
            {
                foreach (XUser obj in objAry)
                {
                    string isActive = obj.User.isActive ? "是" : "否";
                    this.dgvData.Rows.Add(obj.User.code, obj.User.id, obj.User.name, obj.User.sex, obj.User.department, obj.DepartmentName, isActive, obj.User.nation, obj.User.position, obj.User.title, obj.User.wageGrade, obj.User.baseWage, obj.User.postWage, obj.User.memo);
                }
            }
        }

        private void updateDgv(object o, EventArgs arg)
        {
            if (arg != null)
            {
                CommonEventArg<XUser> argObj = (CommonEventArg<XUser>)arg;

                string isActive = argObj.t.User.isActive ? "是" : "否";
                object[] ary =  { argObj.t.User.code, argObj.t.User.id, argObj.t.User.name, argObj.t.User.sex, argObj.t.User.department, argObj.t.DepartmentName,isActive,
                                    argObj.t.User.nation,argObj.t.User.position,argObj.t.User.title,argObj.t.User.wageGrade,argObj.t.User.baseWage,argObj.t.User.postWage,argObj.t.User.memo};
                if (argObj.OpType == Common.OperateType.Add)
                {
                    this.dgvData.Rows.Add(ary);
                }
                else if (argObj.OpType == Common.OperateType.Update)
                {
                    this.dgvData.SelectedRows[0].SetValues(ary);
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            EditUserFrm EditUserFrm = new EditUserFrm();
            EditUserFrm.codeRule = this.codeRule;
            EditUserFrm.EditMode = true;
            EditUserFrm.ShowDialog();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            EditUserFrm EditUserFrm = new EditUserFrm();
            EditUserFrm.EditMode = false;
            EditUserFrm.ShowDialog();
        }

        private void btnfind_Click(object sender, EventArgs e)
        {
            bindGV(null);
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.dgvData.SelectedRows.Count > 0)
            {
                if (MessageBox.Show("确认删除该项吗？", "删除提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    bool delOk = this.BaseDataProxy.DeleteUser(Convert.ToInt32(this.dgvData.SelectedRows[0].Cells["id"].Value.ToString()));
                    if (delOk)
                    {
                        this.dgvData.Rows.Remove(this.dgvData.SelectedRows[0]);
                    }
                }
            }
        }

        private void uTreeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Tag !=null && e.Node.Tag.ToString() != string.Empty)
            {
                bindGV(Convert.ToInt32(e.Node.Tag));
            }
            else
                bindGV(null);
        }

        private void uDataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            //if (this.dgvData.SelectedRows.Count > 0)
            //{
                if (e.RowIndex < 0)
                    return;
                EditUserFrm frm = new EditUserFrm();
                frm.id = Convert.ToInt32(this.dgvData.Rows[e.RowIndex].Cells["id"].Value.ToString());
                if (this.dgvData.Rows[e.RowIndex].Cells["departmentname"].Value != null && this.dgvData.Rows[e.RowIndex].Cells["departmentname"].Value.ToString().Trim() != "")
                    frm.DepName = this.dgvData.Rows[e.RowIndex].Cells["departmentname"].Value.ToString().Trim();
                if (this.dgvData.Rows[e.RowIndex].Cells["department"].Value != null && this.dgvData.Rows[e.RowIndex].Cells["department"].Value.ToString().Trim() != "")
                    frm.DepId = Convert.ToInt32(this.dgvData.Rows[e.RowIndex].Cells["department"].Value.ToString().Trim());
                frm.EditMode = false;
                frm.ShowDialog(this);
            //}
        }

        protected override void OnFormClosed(FormClosedEventArgs e)
        {
            base.OnFormClosed(e);
            GRWL.BZSC.Common.EventCommon.UserUdDGVEvnt += new GRWL.BZSC.Common.EventCommon.UpdateDataGridViewDlgt(updateDgv);
        }

        private void uDataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex < 0 || e.ColumnIndex < 0 || dgvData.RowsCount < 0)
                return;

            SelectItem item = new SelectItem();

            item.Key = this.dgvData.RCText(e.RowIndex, "id");
            item.Name = this.dgvData.RCText(e.RowIndex, "name");
            item.Value = this.dgvData.RCText(e.RowIndex, "code");
            this.SelectorItem = item;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            if (this.dgvData.SelectedRows.Count <= 0 || dgvData.RowsCount < 0)
                return;

            SelectItem item = new SelectItem();
            //if (this.uTreeView1.SelectedNode.Tag != null)

            item.Key = this.dgvData.SelectedRows[0].Cells["code"].Value.ToString();// this.dgvData.RCText(e.RowIndex, "id");// this.uTreeView1.SelectedNode.Tag.ToString();
            item.Name = this.dgvData.SelectedRows[0].Cells["name"].Value.ToString();// this.dgvData.RCText(e.RowIndex, "name");//this.uTreeView1.SelectedNode.Text;
            item.Value = this.dgvData.SelectedRows[0].Cells["id"].Value.ToString();// this.dgvData.RCText(e.RowIndex, "code");//this.uTreeView1.SelectedNode.Name;
            this.SelectorItem = item;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                CodeRuleFrm frm = new CodeRuleFrm("人员信息编码规则");
                frm.Rule = this.codeRule;
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    this.codeRule = frm.Rule;
                }
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
                this.codeRule.autoCode = true;
            else
                this.codeRule.autoCode = false;
        }
    }
}
